<template>
  <el-dialog
    v-el-drag-dialog
    title="待审核商品"
    :visible.sync="dialogVisible"
    width="60%"
    @close="reset"
  >
    <el-checkbox-group v-model="checkList" @change="handleCheckAllChange">
      <el-checkbox label="一级审核" />
      <el-checkbox label="二级审核" />
    </el-checkbox-group>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      style="margin-top: 15px;"
      border
      stripe
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      height="300"
      highlight-current-row
      :total="tableData.total"
      custom-column
      @selection-change="handleSelectionChange"
    />
    <!-- @fetch="fetchData" -->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="success1">一级审核</el-button>
      <el-button type="primary" @click="success2">二级审核</el-button>
      <el-button type="primary" @click="success3">审核（终极）</el-button>
      <el-button type="primary" @click="dialogVisible = false">返 回</el-button>
    </span>
  </el-dialog>
</template>

<script>

  import { CmsSpecialPriceQueryUnCheckPrice, CmsSpecialPriAudit } from '@/api'
  import { parseTime } from '@/utils'
  export default {
    name: 'waitAudit',
    props: ['parentFetchData'],
    data() {
      return {
        dialogVisible: false,
        checkList: [],
        multipleSelection: [],
        priceTypeMap: [{ 'value': 1, 'desc': '下浮', 'key': 'Fluctuating' }, { 'value': 2, 'desc': '固定', 'key': 'Fixed' }],
        query: {},
        tableData: {
          data: [],
          column: [
            { type: 'selection' },
            { label: '客户名称', prop: 'csmName', width: 120, align: 'center' },
            { label: '生效机构', prop: 'scopeOrgName', width: 120, align: 'center' },
            { label: '商品名称', prop: 'goodsName', width: 180, align: 'center' },
            { label: '波动方式', prop: 'priceType', width: 80, align: 'center',
              render: (h, { row }) => <span>{row.priceType && this.priceTypeMap.find(l => l.value === row.priceType).desc }</span>
            },
            { label: '价格', prop: 'price', width: 80, align: 'right' },
            { label: '有效开始时间', prop: 'startDt', width: 120, align: 'center',
              render: (h, { row }) => <span>{row.startDt && parseTime(row.startDt, '{y}-{m}-{d} {h}:{i}:{s}')}</span>
            },
            { label: '有效结束时间', prop: 'endDt', width: 120, align: 'center',
              render: (h, { row }) => <span>{row.endDt && parseTime(row.endDt, '{y}-{m}-{d} {h}:{i}:{s}')}</span>
            },
            { label: '状态', prop: 'checkStatus', width: 120, align: 'center',
              render: (h, { row, column }) => <span>{this.statusMap(row, column) }</span>
            }
          ]
        }
      }
    },
    methods: {
      async open(row) {
        this.dialogVisible = true
        this.query = { ...row }
        console.log(row, 'row')
        await this.fetchData()
      },
      async fetchData() {
        const params = {
          csmId: this.query.csmId,
          scopeOrgSNNo: this.query.orgSNNo
          // currentPage,
          // pageSize
        }
        const res = await CmsSpecialPriceQueryUnCheckPrice(params)
        this.tableData.data = res.data
        // if (currentPage === 1) this.tableData.total = res.data.total
      },
      statusMap(row, column) {
        if (row.endDt < row.nowDt) {
          return '已失效'
        }
        const val = row[column.property]
        if (val === 0) {
          return '一级待审'
        } else if (val === 1) {
          return '二级待审'
        } else if (val === 2) {
          return '已生效'
        }
      },
      handleCheckAllChange(val) {
        if (val.includes('一级审核')) {
          var list = this.tableData.data.filter(l => l.checkStatus === 0)
          this.checkTable(list, true)
        } else {
          list = this.tableData.data.filter(l => l.checkStatus === 0)
          this.checkTable(list, false)
        }
        if (val.includes('二级审核')) {
          list = this.tableData.data.filter(l => l.checkStatus === 1)
          this.checkTable(list, true)
        } else {
          list = this.tableData.data.filter(l => l.checkStatus === 1)
          this.checkTable(list, false)
        }
      },
      checkTable(list, bool) {
        if (list && list.length >= 0) {
          list.forEach(row => {
            this.$refs.lbTable.toggleRowSelection(row, bool)
          })
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
        // console.log(val, 'val')
      },
      success1() {
        var val = this.multipleSelection.map(l => { return { categoryDetailId: l.categoryDetailId, checkStatus: l.checkStatus } })
        // console.log(val)
        if (!val || !val.length > 0) {
          this.$message.warning('请先选择要操作的数据')
          return
        }
        if (val.find(l => l.checkStatus !== 0)) {
          this.$message.warning('当前选择含有不是一级待审数据')
          return
        }
        this.$confirm(`确定审核吗？`, '提示', {
          type: 'info'
        }).then(() => {
          var params = {
            categoryDetailId: val.map(l => l.categoryDetailId).join(','),
            checkStatus: 1
          }
          try {
            CmsSpecialPriAudit(params).then(data => {
              this.fetchData()
            })
          } catch (e) {
            console.log(e)
          }
        })
      },
      success2() {
        var val = this.multipleSelection.map(l => { return { categoryDetailId: l.categoryDetailId, checkStatus: l.checkStatus } })
        // console.log(val)
        if (!val || !val.length > 0) {
          this.$message.warning('请先选择要操作的数据')
          return
        }
        if (val.find(l => l.checkStatus !== 1)) {
          this.$message.warning('当前选择含有不是二级待审数据')
          return
        }
        this.$confirm(`确定审核吗？`, '提示', {
          type: 'info'
        }).then(() => {
          var params = {
            categoryDetailId: val.map(l => l.categoryDetailId).join(','),
            checkStatus: 2
          }
          try {
            CmsSpecialPriAudit(params).then(data => {
              this.fetchData()
            })
          } catch (e) {
            console.log(e)
          }
        })
      },
      success3() {
        var val = this.multipleSelection.map(l => { return { categoryDetailId: l.categoryDetailId, checkStatus: l.checkStatus } })
        // console.log(val)
        if (!val || !val.length > 0) {
          this.$message.warning('请先选择要操作的数据')
          return
        }
        this.$confirm(`确定审核吗？`, '提示', {
          type: 'info'
        }).then(() => {
          var params = {
            categoryDetailId: val.map(l => l.categoryDetailId).join(','),
            checkStatus: 2
          }
          try {
            CmsSpecialPriAudit(params).then(data => {
              this.fetchData()
            })
          } catch (e) {
            console.log(e)
          }
        })
      },
      reset() {
        this.parentFetchData({})
      }
    }
  }
</script>

